"""
服务依赖注入模块
优化服务实例化，避免重复创建实例
"""

from typing import Generator
from sqlalchemy.orm import Session
from fastapi import Depends

from app.services.auth_service import AuthService
from app.services.user_service import UserService
from app.services.role_service import RoleService
from app.services.permission_service import PermissionService
from app.api.deps import get_db


def get_auth_service(db: Session = Depends(get_db)) -> Generator[AuthService, None, None]:
    """获取认证服务实例"""
    service = AuthService(db)
    try:
        yield service
    finally:
        # 清理资源（如果需要）
        pass


def get_user_service(db: Session = Depends(get_db)) -> Generator[UserService, None, None]:
    """获取用户服务实例"""
    service = UserService(db)
    try:
        yield service
    finally:
        # 清理资源（如果需要）
        pass


def get_role_service(db: Session = Depends(get_db)) -> Generator[RoleService, None, None]:
    """获取角色服务实例"""
    service = RoleService(db)
    try:
        yield service
    finally:
        # 清理资源（如果需要）
        pass


def get_permission_service(db: Session = Depends(get_db)) -> Generator[PermissionService, None, None]:
    """获取权限服务实例"""
    service = PermissionService(db)
    try:
        yield service
    finally:
        # 清理资源（如果需要）
        pass


# 组合服务依赖（用于需要多个服务的场景）
def get_auth_and_user_services(
    auth_service: AuthService = Depends(get_auth_service),
    user_service: UserService = Depends(get_user_service)
) -> tuple[AuthService, UserService]:
    """获取认证和用户服务组合"""
    return auth_service, user_service